package com.fadadianqi.logistics.search;

import com.fadadianqi.logistics.customer.model.Customer;
import com.fadadianqi.search.AbstractSearcher;
import com.fadadianqi.search.Argument;
import com.fadadianqi.search.IndexPathHelper;
import com.fadadianqi.search.IndexPathHelper.IndexEnum;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.StringHelper;
import com.yeqiangwei.commons.util.Utils;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.highlight.Highlighter;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service("customerSearcher")
public class CustomerSearcher extends AbstractSearcher {

    private IndexPathHelper indexPathHelper;

    public CustomerSearcher() {
        indexPathHelper = new IndexPathHelper(IndexEnum.CUSTOMER);
    }

    @Override
    protected String getIndexPath() {
        return indexPathHelper.getIndexPath();
    }

    @Override
    protected Argument initializtionArgument(String keyword,
                                             Pagination pagination) {

        Argument argument = Argument.getInstance();
        argument.setPagination(pagination);

        argument.setArgs(Customer.PROP_NAME, keyword, Occur.SHOULD, 0.9F);

        if (!Utils.isLetter(keyword)) {
            String pinyin = StringHelper.toPinyin(keyword);
            argument.setArgs(Customer.PROP_PINYIN, pinyin, Occur.SHOULD, 0.9F);
        } else {
            argument.setArgs(Customer.PROP_PINYIN, keyword, Occur.SHOULD, 0.9F);
        }

        argument.setArgs(Customer.PROP_INITIAL, keyword, Occur.SHOULD, 0.9F);

        argument.setArgs(Customer.PROP_TOWN_NAME, keyword, Occur.SHOULD, 0.6F);
        argument.setArgs(Customer.PROP_VILLAGE_NAME, keyword, Occur.SHOULD, 0.6F);

        argument.setArgs(Customer.PROP_MEMO, keyword, Occur.SHOULD, 0.5F);

        return argument;
    }

    @Override
    protected Object documentToVO(IndexSearcher searcher, Document doc,
                                  Highlighter highlighter) throws IOException {
        CustomerVO c = new CustomerVO();
        c.setName(doc.get("name"));
        c.setId(doc.get("id"));
        c.setTownId(doc.get("townId"));
        c.setTownName(doc.get("townName"));
        c.setVillageId(doc.get("villageId"));
        c.setVillageName(doc.get("villageName"));
        c.setTel(doc.get("tel"));
        c.setMobile(doc.get("mobile"));
        c.setMemo(doc.get(Customer.PROP_MEMO));
        return c;
    }

    public static class CustomerVO implements java.io.Serializable {

        /**
         *
         */
        private static final long serialVersionUID = 9178199051382717700L;

        private String id;
        private String name;
        private String townId;
        private String townName;
        private String villageId;
        private String villageName;
        private String mobile;
        private String tel;
        private String memo;

        public String getMemo() {
            return memo;
        }

        public void setMemo(String memo) {
            this.memo = memo;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public String getTel() {
            return tel;
        }

        public void setTel(String tel) {
            this.tel = tel;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getTownId() {
            return townId;
        }

        public void setTownId(String townId) {
            this.townId = townId;
        }

        public String getTownName() {
            return townName;
        }

        public void setTownName(String townName) {
            this.townName = townName;
        }

        public String getVillageId() {
            return villageId;
        }

        public void setVillageId(String villageId) {
            this.villageId = villageId;
        }

        public String getVillageName() {
            return villageName;
        }

        public void setVillageName(String villageName) {
            this.villageName = villageName;
        }
    }

}
